package com.lee.common.util;

/**
 * 基于唯一主键id生成唯一邀请码
 *
 * @author Lee HN
 * @date 2020/7/23 14:05
 */
public final class InviteCodeUtil {

    private static final char[] chars = {'J', '4', 'B', 'X', 'E', 'V', 'F', 'G', 'W', 'I', 'A', 'K',
            '7', '5', 'N', 'P', 'Q', 'R', 'S', '3', 'U', '1', 'C', '2', 'T', '6', 'H', 'Y', '8', 'M', 'L', 'D'};

    public static String generateCode(long id, int len) {
        char[] a = new char[len];
        for (int i = 0; i < a.length; i++) {
            int pow = (int) Math.pow(chars.length, i);
            a[i] = charAtStuff((int) (id / pow % chars.length) + i);
        }
        return String.valueOf(a);
    }

    private static char charAtStuff(int index) {
        return index < chars.length ? chars[index] : chars[index - chars.length];
    }

}
